package charlie.shop.misc;

import javax.servlet.http.HttpServletRequest;

/**
 * Additional class for paging
 * 
 * @author Vitaliy Mostipaka <imprtat@gmail.com>
 */
public class Common {
    /**
     * Get current page from request
     * 
     * @param request
     * @return 
     */
    static public int getPage(HttpServletRequest request) {
        int page = getParametrInt(request, "page", 1);
        if (page < 1) {
            page = 1;
        }
        return page;
    }
    /**
     * Convert parameter from string to integer
     * @param request
     * @param name
     * @param def
     * @return 
     */
    static public int getParametrInt(HttpServletRequest request, String name, int def) {
        try {
            return Integer.parseInt(request.getParameter(name));
        } catch(NumberFormatException ex) {
            return def;
        }
    }
    
    static public int getParametrInt(HttpServletRequest request, String name) {
        return getParametrInt(request, name, 0);
    }
}
